<template>
	<a-row :gutter="10" class="mb-2">
		<a-col :span="4">
			<Group height="calc(71vh)" :selectTree="selectTree" relationTypeCode="WORKFLOW" :productId="productId"></Group>
		</a-col>
		<a-col :span="10">
			<a-row :gutter="10" class="mb-2">
				<a-col :span="24">
					<a-card>
						<a-space>
							<a-input-search placeholder="请输入内容" v-model:value="keywords" @search="refresh"></a-input-search>
						</a-space>
					</a-card>
				</a-col>
			</a-row>

			<a-row :gutter="10" class="mb-2">
				<a-col :span="24">
					<a-card>
						<h-table
							height="calc(70vh)"
							:rowClick="workflowClick"
							:columns="columns"
							ref="tableRef"
							:data="queryList"
						></h-table>
					</a-card>
				</a-col>
			</a-row>
		</a-col>

		<a-col :span="10">
			<a-row :gutter="10" class="mb-2">
				<a-col :span="24">
					<a-card>
						<a-space>
							<a-input-search
								placeholder="请输入内容"
								v-model:value="instanceKeywords"
								@click="instanceTableRef.refresh()"
							></a-input-search>
							<a-button type="primary" @click="addStart">新增</a-button>
						</a-space>
					</a-card>
				</a-col>
			</a-row>

			<a-row :gutter="10" class="mb-2">
				<a-col :span="24">
					<a-card>
						<h-table
							height="calc(70vh)"
							:columns="instanceColumns"
							ref="instanceTableRef"
							:data="queryInstanceList"
						></h-table>
					</a-card>
				</a-col>
			</a-row>
		</a-col>
	</a-row>

	<a-modal v-model:open="addFlag" width="500px" :closable="false">
		<a-form :model="workflowModel">
			<a-form-item label="启动参数">
				<a-input v-model:value="workflowModel.param" autocomplete="off"></a-input>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button @click="addFlag = false">取 消</a-button>
			<a-button type="primary" @click="save">确 定</a-button>
		</template>
	</a-modal>

	<a-modal v-model:open="submitFlag" width="500px" :closable="false">
		<a-form :model="workflowModel">
			<a-form-item label="节点编码">
				<a-input v-model:value="workflowModel.nodeCode" autocomplete="off"></a-input>
			</a-form-item>
		</a-form>
		<a-form :model="workflowModel">
			<a-form-item label="参数">
				<a-input v-model:value="workflowModel.param" autocomplete="off"></a-input>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button @click="submitFlag = false">取 消</a-button>
			<a-button type="primary" @click="submitNode">确 定</a-button>
		</template>
	</a-modal>

	<processLog
		:hiddenFlag="true"
		:logViewFlag="logViewConfig.logViewFlag"
		:logStoreTypeCode="logViewConfig.logStoreTypeCode"
		:tid="logViewConfig.tid"
	></processLog>
</template>

<script setup>
	import { getCurrentInstance, h, onMounted, ref } from 'vue'
	import Group from '@/components/Group/index.vue'
	import workflowApi from '@/api/workflow/workflowApi.js'
	import workflowInstanceApi from '@/api/workflow/workflowInstanceApi.js'

	import { message } from 'ant-design-vue'
	import { useRouter } from 'vue-router'
	import ProcessLog from '@/views/process/processLog.vue'

	const props = defineProps(['hiddenFlag', 'id'])
	const keywords = ref()
	const currentGroupId = ref(0)
	const productId = ref()
	const tableRef = ref()
	const addFlag = ref(false)
	const instanceKeywords = ref()
	const submitFlag = ref(false)
	const instanceTableRef = ref()
	const currentWorkflowId = ref()
	const logViewConfig = ref({
		logViewFlag: false,
		tid: null,
		logStoreTypeCode: 'db'
	})

	const workflowModel = ref({})
	const selectTree = (id, code, name) => {
		currentGroupId.value = id
		tableRef.value.refresh()
	}
	const addStart = () => {
		if (!currentWorkflowId.value) {
			message.info('请选择工作流')
			return
		}
		addFlag.value = true
		workflowModel.value = {}
	}
	const instanceColumns = ref([
		{
			title: '状态',
			key: 'status',
			dataIndex: 'status',
			ellipsis: true,
			mapping: [
				{
					//0.未开始 1.进行中 2.已完成 3.失败
					key: '0',
					value: '未开始'
				},
				{
					key: '1',
					value: '进行中'
				},
				{
					key: '2',
					value: '已完成'
				},
				{
					key: '3',
					value: '失败'
				}
			]
		},
		{
			title: '开始日期',
			key: 'startTime',
			dataIndex: 'startTime',
			ellipsis: true
		},
		{
			title: '结束日期',
			key: 'endTime',
			dataIndex: 'endTime',
			ellipsis: true
		},
		{
			title: '操作',
			key: 'action',
			width: 250,
			dataIndex: 'action',
			ellipsis: true,
			fixed: 'right',
			buttonList: [
				{
					click: (field, record) => {
						handlerView(record)
					},
					render: (record) => {
						return '查看'
					}
				},
				{
					click: (field, record) => {
						handlerSubmit(record)
					},
					render: (record) => {
						if (record.status == '1') {
							return '提交'
						}
						return null
					}
				},
				{
					type: 'primary',
					click: (field, record) => {
						next(record)
					},
					render: (record) => {
						if (record.status == '1') {
							return '下一步'
						}
						return null
					}
				}
			]
		}
	])
	const columns = ref([
		{
			title: '名称',
			key: 'workflowName',
			dataIndex: 'workflowName',
			ellipsis: true
		},
		{
			title: '编码',
			key: 'workflowCode',
			dataIndex: 'workflowCode',
			ellipsis: true
		}
	])

	const handlerSubmit = (record) => {
		submitFlag.value = true
		workflowModel.value = {}
		workflowModel.value.id = record.id
	}

	const submitNode = async () => {
		let d = await workflowInstanceApi.nextByNodeCode({
			...workflowModel.value
		})
		workflowModel.value = {}
	}
	const next = async (record) => {
		let d = await workflowInstanceApi.next({
			id: record.id,
			param: null
		})
	}
	const handlerView = async (record) => {
		logViewConfig.value.logViewFlag = false
		//获取tid
		let instanceDO = await workflowInstanceApi.getById({
			id: record.id
		})

		logViewConfig.value.logViewFlag = true
		logViewConfig.value.tid = instanceDO.tid
	}
	const queryInstanceList = async () => {
		if (!currentWorkflowId.value) {
			return []
		}
		let instances = await workflowInstanceApi.list({
			workflowId: currentWorkflowId.value
		})
		return instances
	}

	const workflowClick = (record) => {
		currentWorkflowId.value = record.id
		instanceTableRef.value.refresh()
	}

	const queryList = async () => {
		return await workflowApi.list({ groupId: currentGroupId.value })
	}
	const refresh = () => {
		tableRef.value.refresh()
	}
	onMounted(() => {})

	const save = () => {
		workflowInstanceApi.start({
			param: workflowModel.value.param,
			workflowId: currentWorkflowId.value
		})
		addFlag.value = false
	}
</script>

<style scoped></style>
